package com.home.consumer;
import com.home.ex.RabbitmqConfigDirect;
import com.home.ex.RabbitmqConfigFanout;
import com.home.ex.RabbitmqConfigTopics;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 配置消费者
 */
@Component
public class ReceiveHandler {
    //监听NAME_HELLO队列
    @RabbitListener(queues = {RabbitmqConfigTopics.QUEUE_TOPIC_1})
    public void receiveHelloQueueMessage(String msg, Message message, Channel channel) throws IOException {
        System.out.println("pay:"+msg);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //签收消息：指定消息的tags ，以及不做批量签收
            channel.basicAck(deliveryTag, false);
        }
        catch (Exception e) {
            channel.basicNack(deliveryTag, false, true);
        }
    }

    //监听NAME_HELLO队列
    @RabbitListener(queues = {RabbitmqConfigTopics.QUEUE_TOPIC_2})
    public void receive2(String msg, Message message, Channel channel) {
        System.out.println("order:"+msg);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //签收消息：指定消息的tags ，以及不做批量签收
            channel.basicAck(deliveryTag, false);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
